package com.example.video.controller;

import com.example.video.entity.Account;
import com.example.video.service.IAccountService;
import com.example.video.common.Result;
import com.example.video.conf.ScheduledTasks;
import com.example.video.entity.OuterAccount;
import com.example.video.service.IOuterAccountService;
import com.example.video.util.RoleUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/test")
public class Test {

    @Resource
    private IAccountService accountService;
    @Resource
    private IOuterAccountService outerAccountService;

    @Resource
    private ScheduledTasks tasks;

    @Resource
    private AdminController adminController;

    @PostMapping("/change")
    public String test() {
        tasks.changeVideoStatus();
        return "change";
    }


    @GetMapping("testSchedule")
    public String Test(){
        adminController.setSchedule("12","12");
        return "success";
    }


    @GetMapping("/test")
    public String test1(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        // 获取用户的所有权限
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

        // 提取并格式化角色
        List<String> formattedRoles = RoleUtils.extractAndFormatRoles(authorities);
        log.info("AccountSrvImp -> search ->msg: formateRoles"+formattedRoles);
        log.info("AccountSrvImp -> search ->msg: formateRoles"+formattedRoles.get(0));





        return "成功！";
    }

    @PostMapping("/testJson")
    public Result<List<OuterAccount>> testJson(String deviceId){
        Result<List<OuterAccount>> rs = new Result<>();
        Account account = accountService.getItemByDeviceId(deviceId);
        log.info("DevCtr -> /get/outer ->msg: id = "+deviceId);
        log.info("DevCtr -> /get/outer ->msg: account = "+account);
        List<OuterAccount> list = outerAccountService.getListByAccountId(account.getId());
        log.info("DevCtr -> /get/outer ->msg: list = " + list);
        if (list.isEmpty()) {
            rs.setCode(-1);
            rs.setData(null);
            rs.setMsg("获取失败！");
            rs.setCount(0);
        } else {
            rs.setCode(0);
            rs.setData(list);
            rs.setMsg("获取成功！");
            rs.setCount(list.size());
        }
        return rs;
    }




//    @PostMapping("/createDevice")
//    public String createDevice(){
//        return Operate.createDevice().getData();
//    }

}
